home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / COMMONDG.PAK / COMMONDG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  511 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: commdlg.c
  9. //
  10. //  PURPOSE: Implement stub and paint functions for the commdlg sample.
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc         - Processes messages for the main window.
  15. //    MsgCommand      - Handle the WM_COMMAND messages for the main window.
  16. //    MsgPaint        - raw text inside a rectange using attributes set by
  17. //                      the common font and color dialogs.
  18. //    MsgDestroy      - Handles the WM_DESTROY message by calling 
  19. //                      PostQuitMessage().
  20. //    CmdExit         - Handles the file exit command by calling destory 
  21. //                      window on the main window.
  22. //    Open            - (STUB) Open a file.
  23. //    SaveAs          - (STUB) Save a file.
  24. //    GetPageRange    - (STUB) Return the valid range of pages for the current
  25. //                      document.
  26. //    Print           - (STUB) Print a document given the settings from the 
  27. //                      print dialog.
  28. //    FindReplace     - (STUB) Find and/or replace text in the current
  29. //                      document.
  30. //    SetCurrentFont  - Set the current font based on the fonts dialog.
  31. //    SetCurrentColor - Set the current color based on the color dialog.
  32. //
  33. //  COMMENTS:
  34. //
  35. //
  36. //
  37. //  SPECIAL INSTRUCTIONS: N/A
  38. //
  39.  
  40. #include <windows.h>            // required for all Windows applications
  41. #ifdef WIN16
  42. #include "win16ext.h"           // required only for win16 applications
  43. #include "commdlg.h"           
  44. #endif
  45. #include "globals.h"            // prototypes specific to this application
  46. #include "resource.h"
  47.  
  48. // Main window message table definition.
  49. MSD rgmsd[] =
  50. {
  51.     {0,             MsgFindReplace},
  52.     {WM_PAINT,      MsgPaint},
  53.     {WM_COMMAND,    MsgCommand},
  54.     {WM_DESTROY,    MsgDestroy}
  55. };
  56.  
  57. MSDI msdiMain =
  58. {
  59.     sizeof(rgmsd) / sizeof(MSD),
  60.     rgmsd,
  61.     edwpWindow
  62. };
  63.  
  64.  
  65. // Main window command table definition.
  66. CMD rgcmd[] =
  67. {
  68.     {IDM_OPEN,      CmdOpen},
  69.     {IDM_SAVEAS,    CmdSaveAs},
  70.     {IDM_PRINT,     CmdPrint},
  71.     {IDM_PRINTSU,   CmdPrintSU},
  72.     {IDM_FIND,      CmdFindReplace},
  73.     {IDM_REPLACE,   CmdFindReplace},
  74.     {IDM_FONTS,     CmdFonts},
  75.     {IDM_COLORS,    CmdColors},
  76.     {IDM_EXIT,      CmdExit},
  77.     {IDM_ABOUT,     CmdAbout}
  78. };
  79.  
  80. CMDI cmdiMain =
  81. {
  82.     sizeof(rgcmd) / sizeof(CMD),
  83.     rgcmd,
  84.     edwpWindow
  85. };
  86.  
  87.  
  88. static HFONT hfont = NULL;          // The current font
  89. static DWORD rgbFont = RGB(0,0,0);  // The current font color
  90. static DWORD rgbRect = RGB(0,0,0);  // The current rectangle color
  91.  
  92. //
  93. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  94. //
  95. //  PURPOSE:  Processes messages for the main window.
  96. //
  97. //  PARAMETERS:
  98. //    hwnd     - window handle
  99. //    uMessage - message number
  100. //    wparam   - additional information (dependant on message number)
  101. //    lparam   - additional information (dependant on message number)
  102. //
  103. //  RETURN VALUE:
  104. //    The return value depends on the message number.  If the message
  105. //    is implemented in the message dispatch table, the return value is
  106. //    the value returned by the message handling function.  Otherwise,
  107. //    the return value is the value returned by the default window procedure.
  108. //
  109. //  COMMENTS:
  110. //    Call the DispMessage() function with the main window's message dispatch
  111. //    information (msdiMain) and the message specific information.
  112. //
  113.  
  114. LRESULT CALLBACK WndProc(HWND   hwnd, 
  115.                          UINT   uMessage, 
  116.                          WPARAM wparam, 
  117.                          LPARAM lparam)
  118. {
  119.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  120. }
  121.  
  122.  
  123. //
  124. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  125. //
  126. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  127. //
  128. //  PARAMETERS:
  129. //    hwnd     - window handle
  130. //    uMessage - WM_COMMAND (Unused)
  131. //    GET_WM_COMMAND_ID(wparam,lparam)   - Command identifier
  132. //    GET_WM_COMMAND_HWND(wparam,lparam) - Control handle
  133. //
  134. //  RETURN VALUE:
  135. //    The return value depends on the message number.  If the message
  136. //    is implemented in the message dispatch table, the return value is
  137. //    the value returned by the message handling function.  Otherwise,
  138. //    the return value is the value returned by the default window procedure.
  139. //
  140. //  COMMENTS:
  141. //    Call the DispCommand() function with the main window's command dispatch
  142. //    information (cmdiMain) and the command specific information.
  143. //
  144.  
  145. #pragma argsused
  146. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  147. {
  148.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  149. }
  150.  
  151.  
  152. //
  153. //  FUNCTION: MsgPaint(HWND,UINT,WPARAM,LPARAM)
  154. //
  155. //  PURPOSE: Draw text inside a rectange using attributes set by the
  156. //      common font and color dialogs.
  157. //
  158. //  PARAMETERS:
  159. //    hwnd      - Window handle  (Unused)
  160. //    uMessage  - WM_PAINT       (Unused)
  161. //    wparam    - Extra data     (Unused)
  162. //    lparam    - Extra data     (Unused)
  163. //
  164. //  RETURN VALUE:
  165. //    Always returns 0 - Message handled
  166. //
  167. //  COMMENTS:
  168. //
  169. //
  170.  
  171. #pragma argsused
  172. LRESULT MsgPaint(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  173. {
  174.      PAINTSTRUCT ps;
  175.      HDC hdc;
  176.      RECT rc;
  177.      HFONT hfontT = 0;
  178.      DWORD rgbT;
  179.      HBRUSH hbrush;
  180.  
  181.     static char * szText =
  182.         "This sample explores the basic functionality of all of the "
  183.         "windows common dialog boxes.  Most of the common dialogs "
  184.         "provide a user interface used to request information from "
  185.         "the user.  Then this information is used to perform a task "
  186.         "such as opening a file or setting a font.  This sample "
  187.         "presents the dialog, but does not implement the code to "
  188.         "perform most of the related functions.  Instead, a modal "
  189.         "dialog is displayed when an action would normally occur, "
  190.         "describing the action that would take place in a fully "
  191.         "implemented program.";
  192.  
  193.     hdc = BeginPaint(hwnd, &ps);
  194.  
  195.     if (hfont != NULL)
  196.         hfontT = SelectObject(hdc, hfont);
  197.     rgbT = SetTextColor(hdc, rgbFont);
  198.     hbrush = CreateSolidBrush(rgbRect);
  199.  
  200.     GetClientRect(hwnd, &rc);
  201.     InflateRect(&rc, -5, -5);
  202.     FrameRect(hdc, &rc, hbrush);
  203.     DeleteObject(hbrush);
  204.  
  205.      InflateRect(&rc, -5, -5);
  206.     DrawText(hdc,
  207.              szText,
  208.              -1,
  209.              &rc,
  210.              DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
  211.  
  212.     SetTextColor(hdc, rgbT);
  213.     if (hfont != NULL)
  214.         SelectObject(hdc, hfontT);
  215.  
  216.     EndPaint(hwnd, &ps);
  217.  
  218.      return 0;
  219. }
  220.  
  221.  
  222. //
  223. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  224. //
  225. //  PURPOSE: Calls PostQuitMessage().
  226. //
  227. //  PARAMETERS:
  228. //
  229. //    hwnd      - Window handle  (Unused)
  230. //    uMessage  - Message number (Unused)
  231. //    wparam    - Extra data     (Unused)
  232. //    lparam    - Extra data     (Unused)
  233. //
  234. //  RETURN VALUE:
  235. //
  236. //    Always returns 0 - Message handled
  237. //
  238. //  COMMENTS:
  239. //
  240. //
  241.  
  242. #pragma argsused
  243. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  244. {
  245.     if (hfont != NULL)
  246.         DeleteObject(hfont);
  247.      PostQuitMessage(0);
  248.     return 0;
  249. }
  250.  
  251.  
  252. //
  253. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  254. //
  255. //  PURPOSE: Exit the application.
  256. //
  257. //  PARAMETERS:
  258. //    hwnd     - The window.
  259. //    wCommand - IDM_EXIT (unused)
  260. //    wNotify  - Notification number (unused)
  261. //    hwndCtrl - NULL (unused)
  262. //
  263. //  RETURN VALUE:
  264. //    Always returns 0 - command handled.
  265. //
  266. //  COMMENTS:
  267. //
  268. //
  269.  
  270. #pragma argsused
  271. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  272. {
  273.     DestroyWindow(hwnd);
  274.     return 0;
  275. }
  276.  
  277.  
  278. //
  279. //  FUNCTION: Open(char FAR *, HWND)
  280. //
  281. //  PURPOSE: (STUB) Open a file.
  282. //
  283. //  PARAMETERS:
  284. //    szFName - The file to open.
  285. //    hwnd    - The window.
  286. //
  287. //  RETURN VALUE:
  288. //
  289. //  COMMENTS:
  290. //
  291. //
  292.  
  293. VOID Open(char FAR *szFName, HWND hwnd)
  294. {
  295.     MessageBox(hwnd, szFName, "File Open", MB_ICONINFORMATION | MB_OK);
  296. }
  297.  
  298. //
  299. //  FUNCTION: SaveAs(char FAR *, HWND)
  300. //
  301. //  PURPOSE: (STUB) Save a file.
  302. //
  303. //  PARAMETERS:
  304. //    szFName - The file to save.
  305. //    hwnd    - The window.
  306. //
  307. //  RETURN VALUE:
  308. //
  309. //  COMMENTS:
  310. //
  311. //
  312.  
  313. VOID SaveAs(char FAR *szFName, HWND hwnd)
  314. {
  315.     MessageBox(hwnd, szFName, "File Save As",MB_ICONINFORMATION | MB_OK);
  316. }
  317.  
  318.  
  319. //
  320. //  FUNCTION: GetPageRange(VOID)
  321. //
  322. //  PURPOSE: (STUB) Return the valid range of pages for the current document.
  323. //
  324. //  PARAMETERS:
  325. //    NONE.
  326. //
  327. //  RETURN VALUE:
  328. //    LOWORD() - The first printable page.
  329. //    HIWORD() - The last printable page.
  330. //
  331. //  COMMENTS:
  332. //
  333. //
  334.  
  335. DWORD GetPageRange(VOID)
  336. {
  337.      return (DWORD) MAKELONG(1,0xFFFF);
  338. }
  339.  
  340. //
  341. //  FUNCTION: Print(HDC,BOOL,BOOL,BOOL,BOOL,UINT,UINT,UINT)
  342. //
  343. //  PURPOSE: (STUB) Print a document given the settings from the print dialog.
  344. //
  345. //  PARAMETERS:
  346. //    hdc - The printer display context.
  347. //    fRange - Print a page range.
  348. //    fSelection - Print the current selection.
  349. //    fCollate - Collate the copies.
  350. //    fFile - Print to a file.
  351. //    nFromPage - Print from this page (valid only when fRange==TRUE).
  352. //    nToPage - Print to this page (valid only when fRange==TRUE).
  353. //    nCopies - Number of copied to print.
  354. //
  355. //  RETURN VALUE:
  356. //    NONE.
  357. //
  358. //  COMMENTS:
  359. //
  360. //
  361.  
  362. #pragma argsused
  363. VOID Print(HDC     hdc,
  364.               BOOL    fRange,
  365.               BOOL    fSelection,
  366.            BOOL    fCollate, 
  367.            BOOL    fFile,
  368.            UINT    nFromPage, 
  369.            UINT    nToPage, 
  370.            UINT    nCopies,
  371.            HGLOBAL hdns)
  372. {
  373.     char rgchTitle[80];
  374.     char rgchText[100];
  375.     char rgchRange[40];
  376.     LPDEVNAMES lpdns;
  377.     char FAR *lpbDevNames;
  378.  
  379.     lpdns = (LPDEVNAMES)GlobalLock(hdns);
  380.     lpbDevNames = (char FAR *)lpdns;
  381.     wsprintf(rgchTitle,
  382.              "%s;%s;%s",
  383.              lpbDevNames + lpdns->wDriverOffset,
  384.              lpbDevNames + lpdns->wDeviceOffset,
  385.              lpbDevNames + lpdns->wOutputOffset);
  386.     GlobalUnlock(hdns);
  387.  
  388.     if (!fRange)
  389.     {
  390.         if (fSelection)
  391.                 lstrcpy(rgchRange, "Selection");
  392.         else
  393.             lstrcpy(rgchRange, "All");
  394.     }
  395.     else
  396.     {
  397.         wsprintf(rgchRange,"From %d to %d", nFromPage, nToPage);
  398.     }
  399.  
  400.     wsprintf(rgchText,
  401.              "Print %s %s %s (Copies = %d)",
  402.              rgchRange,
  403.              fCollate ? "Collate" : "",
  404.                  fFile ? "File" : "",
  405.              nCopies);
  406.     
  407.     MessageBox(NULL, rgchText, rgchTitle, MB_ICONINFORMATION | MB_OK);
  408. }
  409.  
  410. //
  411. //  FUNCTION: FindReplace(char*,char*,BOOL,BOOL,BOOL,BOOL,BOOL,BOOL)
  412. //
  413. //  PURPOSE: (STUB) Find and/or replace text in the current document.
  414. //
  415. //  PARAMETERS:
  416. //    szFind - The string to find.
  417. //    szReplace - The string used to replace.
  418. //    fFind - Do a find.
  419. //    fReplace - Do a replace.
  420. //    fRepAll - Do a replace all.
  421. //    fDown - Search down if true, up if false.
  422. //    fMatchCase - Match case on a search.
  423. //    fWholeWord - Match whole words only.
  424. //
  425. //  RETURN VALUE:
  426. //    NONE.
  427. //
  428. //  COMMENTS:
  429. //
  430. //
  431.  
  432. VOID FindReplace(char *szFind, 
  433.                  char *szReplace,
  434.                  BOOL fFind, 
  435.                  BOOL fReplace, 
  436.                  BOOL fRepAll,
  437.                  BOOL fDown, 
  438.                  BOOL fMatchCase, 
  439.                  BOOL fWholeWord)
  440. {
  441.     char szTitle[80];
  442.     char szText[80];
  443.  
  444.     wsprintf(szTitle,
  445.              "%s%s%s%s",
  446.              fFind ? "Find " : "",
  447.              fFind ? szFind : "",
  448.              fReplace || fRepAll ? " Replace with " : "",
  449.              fReplace || fRepAll ? szReplace : "");
  450.  
  451.     wsprintf(szText,
  452.              "%s%s%s%s",
  453.              fRepAll ? "Replace All " : "",
  454.              fDown ?  "Down " : "Up ",
  455.              fMatchCase ?  "Match Case " : "",
  456.              fWholeWord ?  "Whole Word " : "");
  457.  
  458.     MessageBox(NULL,
  459.                szText,
  460.                szTitle,
  461.                MB_APPLMODAL | MB_ICONINFORMATION | MB_OK);
  462. }
  463.  
  464.  
  465. //
  466. //  FUNCTION: SetCurrentFont(LPLOGFONT,WORD)
  467. //
  468. //  PURPOSE: Set the current font based on the fonts dialog.
  469. //
  470. //  PARAMETERS:
  471. //    lplf - The font to set
  472. //    rgb - The color to set
  473. //
  474. //  RETURN VALUE:
  475. //    NONE.
  476. //
  477. //  COMMENTS:
  478. //
  479. //
  480.  
  481. VOID SetCurrentFont(LPLOGFONT lplf, DWORD rgb)
  482. {
  483.     if (hfont != NULL)
  484.         DeleteObject(hfont);
  485.     hfont = CreateFontIndirect(lplf);
  486.     rgbFont = rgb;
  487. }
  488.  
  489.  
  490. //
  491. //  FUNCTION: SetCurrentColor(rgb)
  492. //
  493. //  PURPOSE: Set the current color based on the color dialog.
  494. //
  495. //  PARAMETERS:
  496. //    rgb - the color to set
  497. //
  498. //  RETURN VALUE:
  499. //    NONE.
  500. //
  501. //  COMMENTS:
  502. //
  503. //
  504.  
  505. VOID SetCurrentColor(DWORD rgb)
  506. {
  507.     rgbRect = rgb;
  508. }
  509.  
  510.  
  511.